package org.example.JUC;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class ConcurrentHashMapDemo {
    public static ConcurrentHashMap<String, String> concurrentHashMap=new ConcurrentHashMap<>();
    public static AtomicInteger count=new AtomicInteger();

    public static void main(String[] args) {
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        ThreadPoolExecutorDemo.execute(new Task());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(concurrentHashMap.size());
    }

    public static Integer getCount(){
        return count.addAndGet(1);
    }

}

class Task implements Runnable{

    @Override
    public void run() {
        Integer count = ConcurrentHashMapDemo.getCount();
        for (int i = 0; i < 1000; i++) {
            System.out.println("执行任务——" + "key"+count + "value:"+count+":"+i);
            ConcurrentHashMapDemo.concurrentHashMap.put("key"+count+":"+i,"value:"+count+":"+i);
        }
    }
}